package com.xygj.vueblog.controller;

import com.xygj.vueblog.common.lang.Result;
import com.xygj.vueblog.entity.User;
import com.xygj.vueblog.service.UserService;
import freemarker.core.BugException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;
    @GetMapping("/{id}")
    @ResponseBody
    public Result index(@PathVariable int id){
        User user = userService.getById(id);
        return Result.success(200, "操作成功", user);
    }

    @GetMapping(value = "findUser/{name}/{password}",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Result queryUserByName(@PathVariable("name") String name,@PathVariable("password") String password){
        User user = userService.queryUserByName(name,password);
        return Result.success(200, "操作成功", user);

    }

    @RequestMapping(value = "/checkLogin", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public Result checkLogin(@RequestParam("userName") String userName,
                                         @RequestParam("password") String password) {
        Result response = new Result();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);

            Subject subject = SecurityUtils.getSubject();

            subject.login(token);
        }catch (Exception e) {

            Result.fail(400,"失败",null);
            Throwable ex = e.getCause();
            if(ex instanceof BugException) {
                if(ex.getMessage() != null) {
                    Result.fail("失败");
                }
            }else if(e instanceof IncorrectCredentialsException) {
                return Result.fail("密码错误");
            }else {
                return Result.fail("登录失败");
            }
        }

        return response;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result logout(){
        Result response = new Result();
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()) {
            subject.logout();
        }
        return response;
    }

    @RequestMapping(value="/queryUserInfo",method = RequestMethod.GET)
    @ResponseBody
    public Result queryUserInfo() {
        Result response = new Result();
        try {
            User user = userService.getById(11);
            response.setData(user);
        } catch (Exception e) {

            Result.fail(400,"查询失败",null);
        }

        return response;
    }

}
